package com.cy.ppj.web.admin;

import com.cy.ppj.comm.constant.CacheConstants;
import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.manager.ManagerLoginAO;
import com.cy.ppj.model.domian.admin.Manager;
import com.cy.ppj.model.vo.admin.rule.MenuVO;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.service.manager.ManagerService;
import com.cy.ppj.service.rule.RuleService;
import com.cy.ppj.service.statistics.StatisticsService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;


/**
 * @description: 后台公用方法
 * @author: Ocean
 * @create: 2018-07-30 19:01
 **/
@Api(tags = "后台-公用方法")
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminHomeController extends BaseController {

    @Autowired
    private FileService fileService;
    @Autowired
    private ManagerService managerService;
    @Autowired
    private RuleService ruleService;

    @Autowired
    private StatisticsService statisticsService;

    @ApiOperation("维护放款统计")
    @GetMapping("/statistics/loan/amount")
    public Result statisticsLoanAmount(@RequestParam @DateTimeFormat(pattern = Constants.yyyy_MM_dd_HH_mm_ss) Date startTime,
                                       @RequestParam @DateTimeFormat(pattern = Constants.yyyy_MM_dd_HH_mm_ss) Date endTime) {

        statisticsService.addStatisticsLoanAmount(startTime, endTime);

        return ok();
    }

    @ApiOperation("后台文件上传")
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) {

        try {

            return fileService.upload(file);
        } catch (Exception e) {
            log.error("upload error:", e);
        }

        return Result.fail("上传失败");
    }
    
    @ApiOperation("后台文件上传到公共读bucket")
    @PostMapping("/upload/full/bucket")
    public Result uploadToFullBucket(@RequestParam("file") MultipartFile file) {

        try {
            return fileService.uploadToFullBucket(file);
        } catch (Exception e) {
            log.error("upload error:", e);
        }
        return Result.fail("上传失败");
    }

    @ApiOperation("管理员登录")
    @PostMapping("/login")
    public Result login(@Validated ManagerLoginAO managerLoginAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        Manager manager = new Manager();
        BeanUtils.copyProperties(managerLoginAO, manager);
        return managerService.login(manager);
    }

    @ApiOperation("管理员菜单")
    @GetMapping("/menus")
    public Result menus() {

        Long roleId = this.getManager().getRoleId();
        List<MenuVO> menus = ruleService.queryMenusInCahce(roleId);

        return ok(null, menus);
    }

    @ApiOperation("清除缓存")
    @GetMapping("/clear/cache")
    @Caching(evict = {
            @CacheEvict(value = CacheConstants.CACHE_VALUE_15S, allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.CACHE_VALUE_1M, allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.CACHE_VALUE_2M, allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.CACHE_VALUE_1H, allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.CACHE_VALUE_2H, allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.CACHE_RULE_ROLE, allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.CACHE_PRODUCT_TAG, allEntries = true, beforeInvocation = true)
    })
    public Result clearCache() {
        Set<String> keys = this.redisTemplate.keys("*");
        this.redisTemplate.delete(keys);
        return ok();
    }
    
    @ApiOperation("维护注册用户数量统计")
    @GetMapping("/statistics/user/count")
    public Result statisticsUserCount() {
    	Date time = CommonUtils.addTime(-1, Calendar.DAY_OF_YEAR);
        statisticsService.statisticsUser(time);
        return ok();
    }
    
    
}
